/*****************************************************************************//*!
* file  : cantask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"
#include "can_quene.h"

#define _CANTASK_MODULE_
#include "can_task.h"

extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
timer_pulse_type mb_pulse;

void can_filter_cfg(void);
void can_test(void);

void can2_quene_init(void);
void can1_quene_init(void);

typedef struct
{
	CanTxMsgTypeDef can1_tx;
	CanTxMsgTypeDef can2_tx;
	CanRxMsgTypeDef can1_rx0;
	CanRxMsgTypeDef can1_rx1;
	CanRxMsgTypeDef can2_rx0;
	CanRxMsgTypeDef can2_rx1;

	uint8_t can1_tx_req;
	uint8_t can2_tx_req;
	
	uint32_t can1_td[2];
	uint32_t can2_td[2];
	
	uint32_t can1_cmd_dat[16];
	uint32_t can1_sync_cmd_dat[16];
	
	uint32_t baud_cal_us;
} can_test_type;

can_test_type can_tst_obj;

can_baud_cal_type can_baud;

extern simp_can_frame_ctr_type can1_quene;
extern simp_can_frame_ctr_type can2_quene;

/*******************************************************************************
* @brief  task_can
* \param[in] thread_input : argv
* \retval: none
*******************************************************************************/
void task_can(void * thread_input)
{
	uint32_t time_start;

	(void)thread_input;

	can_filter_cfg();

	can_baud.baud_set = 9600;
	can_baud.bs1_max = 16;
	can_baud.bs2_max = 8;
	can_baud.sjw_max = 4;
	can_baud.div_max = 128;

	pulse_init(&mb_pulse, app_tick_get());
	
	can1_quene_init();
	can2_quene_init();
	
	hcan1.pTxMsg = &can_tst_obj.can1_tx;
	hcan1.pRxMsg = &can_tst_obj.can1_rx0;
	hcan1.pRx1Msg = &can_tst_obj.can1_rx1;

	hcan2.pTxMsg = &can_tst_obj.can2_tx;
	hcan2.pRxMsg = &can_tst_obj.can2_rx0;
	hcan2.pRx1Msg = &can_tst_obj.can2_rx1;
	
	while (1)
	{
		osDelay(1);
		
		pulse_create(&mb_pulse, app_tick_get());
		
		can_test();

		can_baud.clk = HAL_RCC_GetPCLK1Freq();

		time_start = app_tick_us_get();
		can_buad_cal(&can_baud);
		can_tst_obj.baud_cal_us = app_tick_us_get() - time_start;

		if(mb_pulse.bPulse_1s)
		{
			can_tst_obj.can1_tx_req = 1;
			//can_tst_obj.can2_tx_req = 1;
		}
		if(mb_pulse.bPulse_20ms)
		{
			//can_tst_obj.can1_tx_req = 1;
			can_tst_obj.can2_tx_req = 1;
		}
		
		if(mb_pulse.bPulse_500ms)
		{
			can_tst_obj.can1_cmd_dat[0] = 0x04030201;
			can_tst_obj.can1_cmd_dat[1] = 0x08070605;
			can_tst_obj.can1_cmd_dat[2] = 0x12111009;
			can_tst_obj.can1_cmd_dat[3] = 0x16151413;
			can_tst_obj.can1_cmd_dat[4] = 0x20191817;
			can_tst_obj.can1_cmd_dat[5] = 0x24232221;
			can_tst_obj.can1_cmd_dat[6] = 0x28272625;
			can_tst_obj.can1_cmd_dat[7] = 0x32313029;
			can_tst_obj.can1_cmd_dat[8] = 0x36353433;
			can_tst_obj.can1_cmd_dat[9] = 0x40393837;
			
			can_tst_obj.can1_sync_cmd_dat[0] = 0x01010101;
			can_tst_obj.can1_sync_cmd_dat[1] = 0x02020202;
			can_tst_obj.can1_sync_cmd_dat[2] = 0x03030303;
		}
		
		can2_quene.cmd_exe_time_interval = 2;
		can1_quene.cmd_exe_time_interval = 2;
		
		simp_can_cmd_poll_mute(&can1_quene);
		
		simp_can_cmd_poll_mute(&can2_quene);
		
		if(mb_pulse.bPulse_1s)
		{
			dbg_print("can1 send: %10d, can2 receive: %10d  #### can2 send: %10d, can1 receive: %10d @ (%d)\n", 
				can1_quene.tx_cnt,
				can2_quene.rx_cnt,  
				can2_quene.tx_cnt,
				can1_quene.rx_cnt,  
				app_tick_get()
			);
		}
		
	}
}

/*******************************************************************************
* @brief  can receive and send
* \param[in] none
* \retval: none
*******************************************************************************/
void can_test(void)
{
	simp_can_frame_type can_frame;
	simp_can_cmd_type cmd;
	
	cmd.dat = can_tst_obj.can1_cmd_dat;
	cmd.id_mode = SIMP_CAN_ID_MODE_EXT;
	cmd.id = 0x660333;
	cmd.len = 64;
	cmd.remot = 0;
	
	simp_can_stc_cmd_req(&can1_quene, 0, &cmd);
	simp_can_stc_cmd_req(&can1_quene, 3, &cmd);
	simp_can_stc_cmd_req(&can1_quene, 6, &cmd);
	
	
	if(mb_pulse.bPulse_1s)
	//if(0)
	{
		cmd.dat = can_tst_obj.can1_sync_cmd_dat;
		cmd.id = 0x660333;
		cmd.len = 12;
		simp_can_sync_cmd_req(&can1_quene, &cmd);
	}

	//can2 send
	if (can_tst_obj.can2_tx_req)
	{
		can_tst_obj.can2_tx_req --;
		
		can_tst_obj.can2_td[0] += 3;
		can_tst_obj.can2_td[1] += 4;		
		
		can_frame.dat[0].uVal = can_tst_obj.can2_td[0];
		can_frame.dat[1].uVal = can_tst_obj.can2_td[1];
		can_frame.id_mode = SIMP_CAN_ID_MODE_EXT;
		can_frame.id = 0x330330;
		can_frame.len = 48;
		can_frame.remot = 0;
		simp_can_send(&can2_quene, &can_frame);
	}
}

